import http from '../../common/request'
import { getState } from '../../common/utils'
import { Md5 } from 'md5-typescript'
import { AxiosResponse } from 'axios'
export class Auth {
  private static instance: Auth
  private constructor() {}

  public static getInstance(): Auth {
    if (!Auth.instance) {
      Auth.instance = new Auth()
    }
    return Auth.instance
  }
  /**
   * 用户登陆
   * @param {登陆信息} param
   * @returns 是否成功登陆
   */
  static async loginOn(param: LoginInfo): Promise<ServiceReturnData<UserInfo>> {
    param.password = `${getState(48)}${Md5.init(param.password)}${getState(
      46,
    )}==`
    const { data: data } = (await http.post(
      '/logincas',
      param,
    )) as AxiosResponse<ServiceReturnData<UserInfo>>
    if (data && data.code == 0) {
      localStorage.setItem('espi-user', JSON.stringify(data.data))
      localStorage.setItem('espi-token', data.token)
    }
    return data
  }
}
